package com.walkline.util.ui;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;

import com.walkline.app.WeiboAppConfig;
import com.walkline.util.BitmapMask;
import com.walkline.util.Enumerations.Themes;
import com.walkline.util.Function;
import com.walkline.util.GPATools;
import com.walkline.weibo.inf.Comment;
import com.walkline.weibo.inf.Status;
import com.walkline.weibo.inf.User;

public class StatusListStyleButtonField extends Field
{
    public static int DRAWPOSITION_TOP = 0;
    public static int DRAWPOSITION_BOTTOM = 1;
    public static int DRAWPOSITION_MIDDLE = 2;
    public static int DRAWPOSITION_SINGLE = 3;

    private static final int CORNER_RADIUS = 0;

    private static boolean needSmallIcon = Display.getWidth() < 640 ? true : false;

    private static final int HPADDING = Display.getWidth() <= 320 ? 4 : 6;
    private static final int VPADDING = 4;

    private static int COLOR_TIME = 0x6F6F6F;
    private static int COLOR_TIME_FOCUS = 0x6F6F6F;
    private static int COLOR_SOURCE = 0x6F6F6F;
    private static int COLOR_SOURCE_FOCUS = 0x6F6F6F;//0X6271FA;
    private static int COLOR_COUNTS = 0x6F6F6F;
    private static int COLOR_COUNTS_FOCUS = 0x6F6F6F;
    private static int COLOR_SCREENNAME = 0xDFDFDF;
    private static int COLOR_SCREENNAME_FOCUS = 0xDFDFDF; //DFDFDF;
    private static int COLOR_STATUS = 0xB4B4B4;
    private static int COLOR_STATUS_FOCUS = 0xB4B4B4; //0xAFAFAF;
    private static int COLOR_RETWEETED = 0x646464;
    private static int COLOR_RETWEETED_FOCUS = 0x646464; //0x7F7F7F;
    private static int COLOR_BACKGROUND = Color.BLACK;
    private static int COLOR_BACKGROUND_FOCUS = 0x1A1A1A; //0x071A37; //0x031A32; //0x062f67;//0x030D1B;
    //private static final int COLOR_BORDER = COLOR_BACKGROUND; //0xBBBBBB;
    //private static final int COLOR_BORDER_FOCUS = COLOR_BACKGROUND_FOCUS;//0xBBBBBB;

    private MyLabelField _labelScreenName;
    private MyLabelFieldWithNewline _labelText;
    private MyLabelField _labelTime;
    private MyLabelField _labelSource;
    private MyLabelField _labelCounts;
    private MyLabelFieldWithNewline _labelText_Retweet;

    private static Font FONT_SCREENNAME = WeiboAppConfig.FONT_SCREENNAME;
	private static Font FONT_SOURCE = WeiboAppConfig.FONT_SOURCE;

    private Bitmap _avatarIcon;
    private Bitmap[] _images_Status = null;
    private Bitmap[] _images_Retweet = null;
    private String[] _status_images = null;
    private String[] _retweeted_images = null;
    private byte[][] _status_images_data = null;
    private byte[][] _retweeted_images_data = null;

    private String _avatar_url = "";
    private byte[] _avatar_data;
    private int _avatar_size = 0;

    private int _rightOffset;
    private int _leftOffset;
    private int _labelHeight;

    private String _id_str = "";
    private Status _status;
    private Status _retweeted_status;
    private Comment _comment;
    private User _user;
    private WeiboAppConfig _appConfig;

    private int _drawPosition = -1;

    public StatusListStyleButtonField(WeiboAppConfig appConfig, Status status, boolean isRepost)
    {
        super(USE_ALL_WIDTH | Field.FOCUSABLE);

        _appConfig = appConfig;

        if (status.isDeleted())
    	{
    		return; //_labelText = new MyLabelFieldWithNewline(status.getText());
    	} else {
	    	String screen_name = status.getUser().getScreenName() + (!status.getUser().getRemark().equals("") ? "(" + status.getUser().getRemark() + ")" : "");
	    	String time = status.getCreatedAtShort();
	    	String text = status.getText();
	    	String source = status.getSource() != null ? status.getSource().getName() : null;
	    	String[] images = status.getPicUrls().getCount() > 1 ? status.getPicUrls().getSquarePics() : status.getPicUrls().getThumbnailPics();
	    	Status status_Retweet = isRepost ? null : status.getRetweetedStatus();
	    	String counts = status.getRepostsCount() > 0 ? "转 " + status.getRepostsCount() : "";
	    	counts += status.getCommentsCount() > 0 ? (!counts.equals("") ? " | " : "") + "评 " + status.getCommentsCount() : "";
	    	counts += status.getAttitudesCount() > 0 ? (!counts.equals("") ? " | " : "") + "赞 " + status.getAttitudesCount() : "";
	
	    	_avatarIcon = Bitmap.getBitmapResource("avatar.png");
	    	_avatar_url = status.getUser().getProfileImageUrl();
	    	_avatar_size = _avatarIcon.getWidth();
	    	_id_str = status.getIdStr();
	    	_status = status;
	    	_retweeted_status = status_Retweet;
	    	_status_images = images;
	    	_user = status.getUser();
	
	        if (screen_name != null) {_labelScreenName = new MyLabelField(screen_name);}
	        if (time != null) {_labelTime = new MyLabelField(time, 0);}
	        if (text != null) {_labelText = new MyLabelFieldWithNewline(text);}
	        if (source != null) {_labelSource = new MyLabelField("来自 " + source, 0);}
	        if (!counts.equals("")) {_labelCounts = new MyLabelField(counts, 0);}
	        if (images != null)
	        {
	        	int count = status.getPicUrls().getCount();
	
	        	if (count > 0)
	        	{
	        		if (needSmallIcon)
	        		{
	        			if (count > 3) {count = 4;}	
	        		} else {
	        			if (count > 5) {count = 6;}
	        		}
	
	            	_status_images_data = new byte[count][];
	            	_images_Status = new Bitmap[count];
	
	            	if (count == 1)
	            	{
	            		_images_Status[0] = Bitmap.getBitmapResource("status_image_single.png");
	            	} else {
	               		for (int i=0; i<count; i++)
	                   	{
	                   		_images_Status[i] = Bitmap.getBitmapResource("status_image_multi.png");
	                   	}
	
	               		if (status.getPicUrls().getCount() > count)
	               		{
	               			_images_Status[count -1] = Bitmap.getBitmapResource("more_status_image.png");
	               		}            		
	            	}
	        	}
	        }
	
	        String[] images_Retweet = null;
	        if (status_Retweet != null)
	        {
	        	if (status_Retweet.isDeleted())
	        	{
	        		_labelText_Retweet = new MyLabelFieldWithNewline(status_Retweet.getText());
	        	} else {
	            	String screenname_Retweet = status_Retweet.getUser().getScreenName();
	            	String text_Retweet = status_Retweet.getText();
	            	images_Retweet = status_Retweet.getPicUrls().getCount() > 1 ? status_Retweet.getPicUrls().getSquarePics() : status_Retweet.getPicUrls().getThumbnailPics();
	            	_retweeted_images = images_Retweet;
	
	                if (screenname_Retweet != null && text_Retweet != null) {_labelText_Retweet = new MyLabelFieldWithNewline("@" + screenname_Retweet + " :" + text_Retweet);}
	                if (images_Retweet != null)
	                {
	                	int count = status_Retweet.getPicUrls().getCount();
	
	                	if (count > 0)
	                	{
	                		if (needSmallIcon)
	                		{
	                			if (count > 3) {count = 4;}	
	                		} else {
	                			if (count > 5) {count = 6;}
	                		}
	
	                    	_retweeted_images_data = new byte[count][];
	                    	_images_Retweet = new Bitmap[count];
	
	                    	if (count == 1)
	                    	{
	                    		_images_Retweet[0] = Bitmap.getBitmapResource("status_image_single.png");
	                    	} else {
	                       		for (int i=0; i<count; i++)
	                           	{
	                           		_images_Retweet[i] = Bitmap.getBitmapResource("status_image_multi.png");
	                           	}
	
	                       		if (status_Retweet.getPicUrls().getCount() > count)
	                       		{
	                       			_images_Retweet[count -1] = Bitmap.getBitmapResource("more_status_image.png");
	                       		}                    		
	                    	}
	                	}
	                }
	        	}
	        }
    	}

        setThemesColor();
        setFontSize();
    }

    public StatusListStyleButtonField(WeiboAppConfig appConfig, Comment comment)
    {
        super(USE_ALL_WIDTH | Field.FOCUSABLE);

        _appConfig =  appConfig;

    	String screen_name = comment.getUser().getScreenName() + (!comment.getUser().getRemark().equals("") ? "(" + comment.getUser().getRemark() + ")" : "");
    	String time = comment.getCreatedAtShort();
    	String text = comment.getText();
    	String source = comment.getSource() != null ? comment.getSource().getName() : null;
    	Status status_Retweet = comment.getStatus();
        Comment reply_comment = comment.getReplyComment();

    	_avatarIcon = Bitmap.getBitmapResource("avatar.png");
    	_avatar_url = comment.getUser().getProfileImageUrl();
    	_avatar_size = _avatarIcon.getWidth();
    	_comment = comment;
    	_user = comment.getUser();

        if (screen_name != null) {_labelScreenName = new MyLabelField(screen_name);}
        if (time != null) {_labelTime = new MyLabelField(time, 0);}
        if (text != null) {_labelText = new MyLabelFieldWithNewline(text);}
        if (source != null) {_labelSource = new MyLabelField("来自 "+source, 0);}

        if (reply_comment != null)
        {
        	String screenname_ReplyTo = reply_comment.getUser().getScreenName();
        	String text_ReplyTo = reply_comment.getText();

        	_labelText_Retweet = new MyLabelFieldWithNewline("@" + screenname_ReplyTo + " :" + text_ReplyTo);
        } else {
            if (status_Retweet != null)
            {
            	if (status_Retweet.isDeleted())
            	{
            		_labelText_Retweet = new MyLabelFieldWithNewline(status_Retweet.getText());
            	} else {
            		String screenname_Retweet = status_Retweet.getUser().getScreenName();
                	String text_Retweet = status_Retweet.getText();

                    if (screenname_Retweet != null && text_Retweet != null) {_labelText_Retweet = new MyLabelFieldWithNewline("@" + screenname_Retweet + " :" + text_Retweet);}
            	}
            }
        }

        setThemesColor();
        setFontSize();
    }

    private void setFontSize()
    {
    	if (_labelScreenName != null) {_labelScreenName.setFont(FONT_SCREENNAME);}
    	if (_labelSource != null) {_labelSource.setFont(FONT_SOURCE);}
    	if (_labelCounts != null) {_labelCounts.setFont(FONT_SOURCE);}
    }

    private void setThemesColor()
    {
    	int[] currentThemesValue = Themes.choicesThemesValue[_appConfig.getThemes()];

    	COLOR_BACKGROUND = currentThemesValue[0];
    	COLOR_BACKGROUND_FOCUS = currentThemesValue[1];
    	COLOR_SCREENNAME = currentThemesValue[2];
    	COLOR_SCREENNAME_FOCUS = currentThemesValue[3];
    	COLOR_STATUS = currentThemesValue[4];
    	COLOR_STATUS_FOCUS = currentThemesValue[5];
    	COLOR_RETWEETED = currentThemesValue[6];
    	COLOR_RETWEETED_FOCUS = currentThemesValue[7];
    	COLOR_TIME = currentThemesValue[8];
    	COLOR_TIME_FOCUS = currentThemesValue[9];
    	COLOR_SOURCE = currentThemesValue[10];
    	COLOR_SOURCE_FOCUS = currentThemesValue[11];
    	COLOR_COUNTS = currentThemesValue[12];
    	COLOR_COUNTS_FOCUS = currentThemesValue[13];
    }

    public String getIdStr() {return _id_str;}

    public User getUser() {return _user;}

    public Status getStatus() {return _status;}

    public Status getRetweetedStatus() {return _retweeted_status;}

    public Comment getComment() {return _comment;}

    public String getAvatarUrl() {return _avatar_url;}

    public void setAvatarIcon(byte[] data)
    {
    	_avatar_data = data;

		if (_avatar_data != null)
		{
			Bitmap newIcon = Bitmap.createBitmapFromBytes(_avatar_data, 0, -1, 1);

			_avatar_size = newIcon.getWidth();
			newIcon = GPATools.ResizeTransparentBitmap(newIcon, _avatar_size, _avatar_size, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_STRETCH);

			BitmapMask mask = new BitmapMask(new XYEdges(5, 5, 5, 5), Bitmap.getBitmapResource("mask.png"));
			mask.applyMask(newIcon);

			_avatarIcon = newIcon;

			invalidate();
		}
    }

    public String[] getStatusImagesUrl() {return _status_images;}

    public byte[] getStatusImageData(int sequence) {return _status_images_data[sequence];}

    public void setStatusImage(byte[] data, int sequence)
    {
    	if (_images_Status != null && data != null)
    	{
    		_status_images_data[sequence] = data;

    		EncodedImage image = EncodedImage.createEncodedImage(data, 0, data.length);

    		if (_images_Status.length == 1)
    		{
    			_images_Status[sequence] = Function.bestFit(image, 120, 120);

    			if (image.getWidth() > image.getHeight())
    			{
    				UiApplication.getUiApplication().invokeLater(new Runnable() {public void run() {updateLayout();}});	
    			} else {
    				invalidate();
    			}
    		} else {
    			_images_Status[sequence] = image.getBitmap();

    			invalidate();
    		}
    	}
    }

    public String[] getRetweetedImagesUrl() {return _retweeted_images;}

    public byte[] getRetweetedImageData(int sequence) {return _retweeted_images_data[sequence];}

    public void setRetweetedImage(byte[] data, int sequence)
    {
    	if (_images_Retweet != null && data != null)
    	{
    		_retweeted_images_data[sequence] = data;

    		EncodedImage image = EncodedImage.createEncodedImage(data, 0, data.length);

    		if (_images_Retweet.length == 1)
    		{
    			_images_Retweet[sequence] = Function.bestFit(image, 120, 120);

    			if (image.getWidth() > image.getHeight())
    			{
    				UiApplication.getUiApplication().invokeLater(new Runnable() {public void run() {updateLayout();}});	
    			} else {
    				invalidate();
    			}
    		} else {
    			_images_Retweet[sequence] = image.getBitmap();

    			invalidate();
    		}
    	}
    }

    public void setDrawPosition( int drawPosition ) {_drawPosition = drawPosition;}

    public int getPreferredHeight() {return _labelHeight;}

    public void layout(int width, int height)
    {
    	_labelHeight = VPADDING;
        _leftOffset = HPADDING;
        _rightOffset = HPADDING;

        if (_avatarIcon != null) {_leftOffset += _avatarIcon.getWidth() + HPADDING;}

        if (_labelTime != null) {_labelTime.layout(width- _leftOffset - _rightOffset, height);}
        
        if (_labelScreenName != null)
        {
        	int intWidth = _labelTime != null ? _labelTime.getWidth() : 0;
        	_labelScreenName.layout(width - _leftOffset - _rightOffset - intWidth, height);
            _labelHeight += _labelScreenName.getHeight();	
        }

        if (_labelText != null)
        {
        	_labelText.layout(width - _leftOffset - _rightOffset, height);
        	_labelHeight += _labelText.getHeight() + VPADDING;

            if (_images_Status != null) {_labelHeight += _images_Status[0].getHeight() + VPADDING;}

            if (_labelText_Retweet != null)
            {
            	_labelText_Retweet.layout(width - _leftOffset - _rightOffset, height);
            	_labelHeight += _labelText_Retweet.getHeight() + 2 * VPADDING;

                if (_images_Retweet != null) {_labelHeight += _images_Retweet[0].getHeight() + VPADDING;}
            }
        }

        if (_avatarIcon != null) {_labelHeight = (_avatarIcon.getHeight() >= _labelHeight) ? _avatarIcon.getHeight() + 2 * VPADDING : _labelHeight;}

        if (_labelSource != null)
        {
        	_labelSource.layout(width - _leftOffset - _rightOffset, height);
        	_labelHeight += _labelSource.getHeight() + 2 * VPADDING;

        	if (_labelCounts != null) {_labelCounts.layout(width - _leftOffset - _rightOffset, height);}
        } else if (_labelCounts != null) {
        	_labelCounts.layout(width - _leftOffset - _rightOffset, height);
        	_labelHeight += _labelCounts.getHeight() + 2 * VPADDING;
        } else {
        	_labelHeight += VPADDING;
        }

        setExtent(width, _labelHeight);
    }

    protected void paint(Graphics g)
    {
    	int currentTop = VPADDING;

        // Avatar Bitmap
        if (_avatarIcon != null) {g.drawBitmap(HPADDING, currentTop, _avatarIcon.getWidth(), _avatarIcon.getHeight(), _avatarIcon, 0, 0);}

        // Screen name Text
        if (_labelScreenName != null)
        {
            try {
            	g.setColor(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? COLOR_SCREENNAME_FOCUS : COLOR_SCREENNAME);
            	g.setFont(FONT_SCREENNAME);
                g.pushRegion(_leftOffset, currentTop, getWidth() - _leftOffset - _rightOffset, _labelScreenName.getHeight(), 0, 0);
                _labelScreenName.paint(g);
            } finally {g.popContext();}
        }

        // Time Text
        if (_labelTime != null)
        {
        	try {
        		g.setColor(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? COLOR_TIME_FOCUS : COLOR_TIME);
        		g.pushRegion(getWidth() - _labelTime.getWidth()-_rightOffset, currentTop, _labelTime.getWidth(), _labelTime.getHeight(),0,0);
        		_labelTime.paint(g);
        	} finally{g.popContext();}
        }

        // Status Text
        if (_labelText != null)
        {
        	if (_labelScreenName != null) {currentTop += _labelScreenName.getHeight();}

        	try {
        		g.setColor(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? COLOR_STATUS_FOCUS : COLOR_STATUS);
        		g.pushRegion(_leftOffset + HPADDING, currentTop, getWidth()-_leftOffset-_rightOffset, _labelText.getHeight(), 0, 0);
        		_labelText.paint(g);
        	} finally{g.popContext();}

        	currentTop += _labelText.getHeight() + VPADDING;

            if (_images_Status != null)
            {
            	int xOffset = _leftOffset + HPADDING;
            	int count = _images_Status.length;

            	if (needSmallIcon)
        		{
        			if (count > 3) {count = 4;}	
        		} else {
        			if (count > 5) {count = 6;}
        		}

            	for (int i=0; i<count; i++)
            	{
            		g.drawBitmap(xOffset, currentTop, _images_Status[i].getWidth(), _images_Status[i].getHeight(), _images_Status[i], 0, 0);
            		xOffset += _images_Status[i].getWidth() + HPADDING * 2;
            	}

           		currentTop += _images_Status[0].getHeight() + VPADDING;
            }
        }

        // Retweeted Status Text
        if (_labelText_Retweet != null)
        {
        	currentTop += VPADDING;

        	try {
        		g.setColor(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? COLOR_RETWEETED_FOCUS : COLOR_RETWEETED);
        		g.pushRegion(_leftOffset+HPADDING, currentTop, getWidth() - _leftOffset-_rightOffset, _labelText_Retweet.getHeight(), 0, 0);
        		_labelText_Retweet.paint(g);
        	} finally{g.popContext();}

        	currentTop += _labelText_Retweet.getHeight() + VPADDING;

            if (_images_Retweet != null)
            {
            	int xOffset = _leftOffset + HPADDING;
            	int count = _images_Retweet.length;

            	if (needSmallIcon)
        		{
        			if (count > 3) {count = 4;}	
        		} else {
        			if (count > 5) {count = 6;}
        		}

            	for (int i=0; i<count; i++)
            	{
            		g.drawBitmap(xOffset, currentTop, _images_Retweet[i].getWidth(), _images_Retweet[i].getHeight(), _images_Retweet[i], 0, 0);
            		xOffset += _images_Retweet[i].getWidth() + HPADDING * 2;
            	}

           		currentTop += _images_Retweet[0].getHeight() + VPADDING;
            }
        }

        // Source
        if (_labelSource != null)
        {
        	if (currentTop < _avatarIcon.getHeight())
        	{
        		currentTop = _avatarIcon.getHeight() + 3 * VPADDING;
        	} else {
        		currentTop += VPADDING;	
        	}

    		try {
    			g.setColor(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? COLOR_SOURCE_FOCUS : COLOR_SOURCE);
    			g.setFont(FONT_SOURCE);
        		g.pushRegion(2 * HPADDING, currentTop, _labelSource.getWidth(), _labelSource.getHeight(), 0, 0);
        		_labelSource.paint(g);
        	} finally{g.popContext();}
        }

        // Reply counts
        if (_labelCounts != null)
		{
    		try {
    			g.setColor(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? COLOR_COUNTS_FOCUS : COLOR_COUNTS);
    			g.setFont(FONT_SOURCE);
        		g.pushRegion(getWidth() - _labelCounts.getWidth() - _rightOffset, currentTop, _labelCounts.getWidth(), _labelCounts.getHeight(), 0, 0);
        		_labelCounts.paint(g);
        	} finally{g.popContext();}
		}
    }

    protected void paintBackground(Graphics g)
    {
        if (_drawPosition < 0)
        {
            super.paintBackground(g);
            return;
        }

        int oldColour = g.getColor();

        int background = g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? COLOR_BACKGROUND_FOCUS : COLOR_BACKGROUND;

        try {
            if (_drawPosition == 0)
            {
                // Top
                g.setColor(background);
                g.fillRoundRect(0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS);
                //g.setColor(COLOR_BORDER);
                g.drawRoundRect(0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS);
                g.drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);
            } else if(_drawPosition == 1) {
                // Bottom 
                g.setColor(background);
                g.fillRoundRect(0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS);
                //g.setColor(COLOR_BORDER);
                g.drawRoundRect(0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS);
            } else if(_drawPosition == 2) {
                // Middle
                g.setColor(background);
                g.fillRoundRect(0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS);
                //g.setColor(COLOR_BORDER);
                g.drawRoundRect(0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS);
                g.drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);
            } else {
                // Single
                g.setColor(background);
                g.fillRoundRect(0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS);
                //g.setColor(COLOR_BORDER);
                g.drawRoundRect(0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS);
            }
        } finally {
            g.setColor(oldColour);
        }
    }

    protected void drawFocus(Graphics g, boolean on)
    {
        if(_drawPosition < 0)
        {
            super.drawFocus(g, on);
        } else {
            boolean oldDrawStyleFocus = g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS);

            try {
                if(on)
                {
                	g.setColor(Color.WHITE);
                    g.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, true);
                }

                paintBackground(g);
                paint(g);
            } finally {
                g.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus);
            }
        }
    }

    protected boolean keyChar(char character, int status, int time) 
    {
        if (character == Characters.ENTER)
        {
            clickButton();
            return true;
        }

        return super.keyChar(character, status, time);
    }

    protected boolean navigationClick(int status, int time) 
    {
        clickButton(); 
        return true;    
    }

    protected boolean trackwheelClick(int status, int time)
    {        
        clickButton();    
        return true;
    }

    protected boolean invokeAction(int action) 
    {
        switch(action)
        {
            case ACTION_INVOKE:
                clickButton(); 
                return true;
        }

        return super.invokeAction(action);
    }

    public void clickButton() {fieldChangeNotify(0);}

    protected boolean touchEvent(TouchEvent message)
    {
        int x = message.getX(1);
        int y = message.getY(1);

        if (x < 0 || y < 0 || x > getExtent().width || y > getExtent().height) {return false;}

        switch (message.getEvent())
        {
        	case TouchEvent.CLICK:
                clickButton();
                return true;
        }

        return super.touchEvent(message);
    }

    public void setDirty( boolean dirty ) {}
    public void setMuddy( boolean muddy ) {}
}